<?php

/**
 * @author PeoneEr
 * @package aion personal cabinet v1.0
 * @copyright Aion Factory Inc. 2010-2011
 * @license GNU v3
 * @link Ai-Factory.ru && http://code.google.com/p/aionprivateofficev2
 */
require_once "core/application/models/activatepremium/mactivatepremium.php";
require_once "core/application/controllers/cmoney.php";
require_once "core/application/classes/errorClass.php";
require_once "core/application/classes/membershipClass.php";
require_once "core/application/classes/selectValueClass.php";

class activatePremium_Controller extends Premium_Activate_Model {

    /**
     * @var static $_instance - getInstance()
     * @var object $_lang - язык
     * @var int $_needBalance - необходимые средства
     * @var int $_newBalance - новый баланс
     * @var array $_params
     * @var int $_balance
     */
    private static $_instance = NULL;
    public $_lang = NULL;
    private $_needBalance = 0;
    private $_newBalance = 0;
    private $_balance = 0;
    private $_params = array();
    private $_expire = NULL;

    public static function getInstance() {
        if (is_null(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class();
        }
        return self::$_instance;
    }

    private function mathExpireTime($expire) {
        /**
         *  дата снятия премиума
         * @param int $expire
         * @return date $end_expire
         */
        $time = mktime(0, 0, 0, date("m"), date("d"), date("Y")); //перевод даты в unixtime
        /**
         * проход по параметрам
         *  1: сутки
         *  2: 2 суток
         *  3: неделя
         *  4: месяц
         * можно менять, разрешаю
         */
        switch ($expire) {
            case 1: $time += 24 * 60 * 60;
                break;
            case 2: $time += 2 * 24 * 60 * 60;
                break;
            case 3: $time += 7 * 24 * 60 * 60;
                break;
            case 4: $time += 31 * 24 * 60 * 60;
                break;
        }
        return date("Y-m-d", $time); //обратный переход, от unixtime в простую дату
    }

    private function catchParams() {
        /**
         *  массив с инфой
         * @return array $premium
         */
        $params = array(
            'login' => $_SESSION['name'],
            'membership' => $_POST['membership'],
            'expiretmp' => $_POST['expire']
        );
        return $params;
    }

    public function setPremium() {
        $this->_lang = $this->getLanguage();
        $this->_params = $this->catchParams();
        $this->_expire = $this->mathExpireTime($this->_params['expiretmp']);
        if (!$this->isPremium($this->_params['login'])) {
            $this->echoError($this->_lang->language['alreadyVipOrPremium']);
        }
        $this->_balance = Cmoney::getInstance()->showMoney();
        /**
         * а это сложный переключатель. переключает сразу два параметра.
         *      1: ВИП или премиум
         *      2: переключает дни
         * соответственно взымает плату
         */
        switch ($this->_params['membership']) {
            case 1: $value = valueSelect::selectValue(2);
                $this->_newBalance = $this->_balance - $value;
                $this->_needBalance = $value;
                switch ($this->_params['expiretmp']) {
                    case 1: $value = valueSelect::selectValue(4);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 2: $value = valueSelect::selectValue(5);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 3: $value = valueSelect::selectValue(6);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 4: $value = valueSelect::selectValue(7);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                }
                break;
            case 2: $value = valueSelect::selectValue(3);
                $this->_newBalance = $this->_balance - $value;
                $this->_needBalance = $value;
                switch ($this->_params['expiretmp']) {
                    case 1: $value = valueSelect::selectValue(10);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 2:
                        $value = valueSelect::selectValue(11);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 3:
                        $value = valueSelect::selectValue(12);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                    case 4:
                        $value = valueSelect::selectValue(13);
                        $this->_newBalance = $this->_newBalance - $value;
                        $this->_needBalance = $this->_needBalance + $value;
                        break;
                }
                break;
        }

        switch ($this->_params['expiretmp']) {
            /**
             *  А вот здесь я забыл прокомментировать.. И так, здесь мы высчитываем, сколько предметов удалять за активацию ВИП или премиум акка на 1,2, 7 и т.п дней. Каждый case - новое значение.
             * @var $value (valueSelect::selectValue(цифра), эта цифра идет в БД, можете смотреть её по ID в таблице personal_settings
             * @var $newItemCount
             * @var $countDeleteItems
             */
            case 1: $value = valueSelect::selectValue(4);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 2: $value = valueSelect::selectValue(5);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 3: $value = valueSelect::selectValue(6);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 4: $value = valueSelect::selectValue(7);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 5: $value = valueSelect::selectValue(10);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 6:
                $value = valueSelect::selectValue(11);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 7:
                $value = valueSelect::selectValue(12);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
            case 8:
                $value = valueSelect::selectValue(13);
                $this->_newBalance = $this->_newBalance - $value;
                $this->_needBalance = $this->_needBalance + $value;
                break;
        }
        //если не хватает денег - идите нахуй
        if ($this->_balance < $this->_needBalance)
            $this->echoError($this->_lang->language['noEnoughItems'] . ". Необходимо " . $this->_needBalance . ", а у Вас " . $this->_balance);
        try {
            $this->setAccountPremium($this->_params['login'], $this->_params['membership'], $this->_expire);
            try {
                Cmoney::getInstance()->updateMoney($this->_newBalance);
                //Делаем выборку по статусу
                //Выводим сообщение, что премиум активирован, говорим сколько вещей удалено и сколько осталось, говорим спасибо
                $this->echoSuccess("
            Аккаунту <b>" . $this->_params['login'] . "</b> успешно присвоен статус 
            <b>" . Membership::switchMembership($this->_params['membership']) . "</b> до 
            <b>" . $this->_expire . "</b>         
            <br /> <b>" . $this->_needBalance . "</b> У.Е было снято с Вашего счета.      
            <br /> Спасибо!");
                unset($this->_params, $this->_lang);
            } catch (Exception $e) {
                echo $e->getMessage();
                error::getInstance($this->_lang->language['defaultError']);
                unset($this->_params, $this->_lang);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            error::getInstance($this->_lang->language['defaultError']);
            unset($this->_params, $this->_lang, $this->_balance);
        }
    }

}

?>
